/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmodcog2.component;

import java.util.Stack;
import jmodcog2.base.Runner;

/**
 *
 * @author Mitch
 */
public class ReceiverNode implements Node {
    private Stack<Runner> origins;
    private Runner target;

    public Runner getTarget() {
        return target;
    }

    public boolean containsOrigin(Runner r) {
        return origins.contains(r);
    }

    public boolean removeOrigin(Runner r) {
        if (target == null) {
            return origins.remove(r);
        }
        return false;
    }

    public boolean addOrigin(Runner r) {
        if (target == null && !origins.contains(r)) {
            origins.add(r);
        }
        return false;
    }
    private float[] data;

    @Override
    public boolean send() {
        int size = originConnectionSize();
        if (data == null || data.length != size) {
            data = new float[size];
        }
        int start = 0;
        for (Runner origin : origins) {
            System.arraycopy(origin.getOutput(), 0, data, start, origin.outputSize());
            start += origin.outputSize();
        }
        return target.process(data);
    }

    @Override
    public int originConnectionSize() {
        if (!origins.empty()) {
            int sum = 0;
            for (Runner origin : origins) {
                sum += origin.outputSize();
            }
            return sum;
        }
        return -1;
    }

    @Override
    public int targetConnectionSize() {
        if (target != null) {
            return target.inputSize();
        }
        return -1;
    }
    
}
